package com.ruoyi.mentor.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mentor.bo.SubjectAddBo;
import com.ruoyi.mentor.bo.SubjectEditBo;
import com.ruoyi.mentor.bo.SubjectQueryBo;
import com.ruoyi.mentor.domain.Subject;
import com.ruoyi.mentor.service.ISubjectService;
import com.ruoyi.mentor.vo.SubjectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 学科，记录学科信息Controller
 *
 * @author LH
 * @date 2021-05-21
 */
@Api(value = "学科，记录学科信息控制器", tags = {"学科，记录学科信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mentor/subject")
public class SubjectController extends BaseController {

    private final ISubjectService iSubjectService;

    /**
     * 查询学科，记录学科信息列表
     */
    @ApiOperation("查询学科，记录学科信息列表")
    @PreAuthorize("@ss.hasPermi('mentor:subject:list')")
    @GetMapping("/list")
    public TableDataInfo<SubjectVo> list(@Validated SubjectQueryBo bo) {
        return iSubjectService.queryPageList(bo);
    }

    /**
     * 导出学科，记录学科信息列表
     */
    @ApiOperation("导出学科，记录学科信息列表")
    @PreAuthorize("@ss.hasPermi('mentor:subject:export')")
    @Log(title = "学科，记录学科信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(@Validated SubjectQueryBo bo) {
        List<SubjectVo> list = iSubjectService.queryList(bo);
        ExcelUtil<SubjectVo> util = new ExcelUtil<SubjectVo>(SubjectVo.class);
        return util.exportExcel(list, "学科，记录学科信息");
    }


    @Log(title = "导入学科信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        iSubjectService.importData(file);
        return AjaxResult.success("导入成功");
    }

    /**
     * 获取导入模板
     * @return
     */
    @GetMapping("/importSubjectTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<SubjectVo> util = new ExcelUtil<SubjectVo>(SubjectVo.class);
        return util.importTemplateExcel("学科表");
    }
    /**
     * 获取学科，记录学科信息详细信息
     */
    @ApiOperation("获取学科，记录学科信息详细信息")
    @PreAuthorize("@ss.hasPermi('mentor:subject:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iSubjectService.queryById(id));
    }

    /**
     * 新增学科，记录学科信息
     */
    @ApiOperation("新增学科，记录学科信息")
    @PreAuthorize("@ss.hasPermi('mentor:subject:add')")
    @Log(title = "学科，记录学科信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult add(@Validated @RequestBody SubjectAddBo bo) {
        return toAjax(iSubjectService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改学科，记录学科信息
     */
    @ApiOperation("修改学科，记录学科信息")
    @PreAuthorize("@ss.hasPermi('mentor:subject:edit')")
    @Log(title = "学科，记录学科信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult edit(@Validated @RequestBody SubjectEditBo bo) {
        return toAjax(iSubjectService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除学科，记录学科信息
     */
    @ApiOperation("删除学科，记录学科信息")
    @PreAuthorize("@ss.hasPermi('mentor:subject:remove')")
    @Log(title = "学科，记录学科信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iSubjectService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }


    /**
     * 获取所有学科列表
     */
    @ApiOperation("获取所有学科列表")
    @GetMapping("/listAll")
    public AjaxResult getAllSubject() {
        List<Subject> list = iSubjectService.list();
        return AjaxResult.success(list);
    }

    /**
     * 获取所有学科列表Map
     */
    @ApiOperation("获取所有学科列表map")
    @GetMapping("/map")
    public AjaxResult getAllSubjectMap() {
        List<Subject> list = iSubjectService.list();
        Map<Long, String> collect = list.stream().collect(Collectors.toMap(Subject::getId, Subject::getSubName));
        return AjaxResult.success(collect);
    }
}
